package cn.itsource.pethome.system.menu.service.impl;

import cn.itsource.pethome.system.menu.domain.Menu;
import cn.itsource.pethome.system.menu.mapper.MenuMapper;
import cn.itsource.pethome.system.menu.service.IMenuService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2023-02-01
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    public static final String MENU_LIST = "menuList";
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Menu> findFirst() {
        return menuMapper.findFirst();
    }

    /**
     * 该方法 只支持两层级
     * @return
     */
    @Override
    public List<Menu> tree() {
        List<Menu> all = menuMapper.findAll();
        //得到所有的顶级
        List<Menu> parents = all.stream()
                .filter(menu -> menu.getParentId() == null)//filter过滤  找顶级
                .collect(Collectors.toList());

        //将子集放入父级里面
        parents.forEach(parent->{
            all.stream()
                    .filter(menu -> menu.getParentId() != null)//filter过滤  找儿子
                    .forEach(menu->{
                if(menu.getParentId() == parent.getId()){
                    parent.getChildren().add(menu);
                }
            });
        });
        return parents;
    }

    @Override
    public List<Long> findMenuByRole(List<Long> roles) {
        return menuMapper.findMenuByRoleIds(roles);
    }

    @Override
    public void add(Menu menu) {
        if(menu.getParentId() != null){
            if(menu.getName() == null){
                throw new BusinessException("菜单名不能为空");
            }
            if(menu.getComponent() == null){
                throw new BusinessException("组件路径不能为空");
            }
            if(menu.getUrl() == null){
                throw new BusinessException("访问URL不能为空");
            }
            if(menu.getIndex() == null){
                throw new BusinessException("排序不能为空");
            }
        }
        super.add(menu);
        redisTemplate.opsForValue().set(MENU_LIST,super.findAll(),1, TimeUnit.DAYS);
    }

    @Override
    public void update(Menu menu) {
        super.update(menu);
        redisTemplate.opsForValue().set(MENU_LIST,super.findAll(),1, TimeUnit.DAYS);
    }

    @Override
    public List<Menu> findAll() {
        if(redisTemplate.opsForValue().get(MENU_LIST) == null){
            List<Menu> all = super.findAll();
            redisTemplate.opsForValue().set(MENU_LIST,all,1, TimeUnit.SECONDS);
            return all;
        }else {
            return (List<Menu>)redisTemplate.opsForValue().get(MENU_LIST);
        }
    }
}
